
## V Figure 2 : plot average spending by risk score V  --------------------------------

fig_Spending_byPredictedMortality <- function(data_all = dt_for_exhibits_all,
                       data_cnr = dt_for_exhibits_cancer,
                       figs_type = "presentation",
                       sample_n = "cancer" ) {
  
  dt_prob_for_density <- 
    rbind(
      data_all[, .(prob_for_report, sample="General Population Sample")],
      data_cnr[, .(prob_for_report, sample="Cancer Sample")]
    )[, sample := factor(sample, levels=c("General Population Sample", "Cancer Sample"))]
  
  
  
  dt_cost_by_phat <- rbind(
    data_cnr[
      ,.(sample = "Cancer Sample",
         cost_type = "Average Monthly Spending  (Including Months After Death)",
         ave_cost_future_365d = (mean(UTL_f365d_total_cost)/366)*31,
         .N),
      by = .(bins = plyr::round_any(prob_for_report, 0.1, ceiling))
      ],
    data_all[
      ,.(sample = "General Population Sample",
         cost_type = "Average Monthly Spending  (Including Months After Death)",
         ave_cost_future_365d = (mean(UTL_f365d_total_cost)/366)*31,
         .N),
      by = .(bins = plyr::round_any(prob_for_report, 0.1, ceiling))
      ],
    data_cnr[
      ,.(sample = "Cancer Sample",
         cost_type = "Average Monthly Spending",
         ave_cost_future_365d = ((sum(UTL_f365d_total_cost)/sum(num_days_lived))*31),
         .N),
      by = .(bins = plyr::round_any(prob_for_report, 0.1, ceiling))
      ],
    data_all[
      ,.(sample = "General Population Sample",
         cost_type = "Average Monthly Spending",
         ave_cost_future_365d = ((sum(UTL_f365d_total_cost)/sum(num_days_lived))*31),
         .N),
      by = .(bins = plyr::round_any(prob_for_report, 0.1, ceiling))
      ]
  )[, bins := bins-0.1][N >= min_obs_num]
  dt_cost_by_phat[, sample := factor(sample, levels=c("General Population Sample",
                                                      "Cancer Sample"))]
  dt_cost_by_phat[, cost_type := factor(cost_type, levels = c("Average Monthly Spending",
                                                              "Average Monthly Spending  (Including Months After Death)"))]
  
  text_dt <- rbind(
    # labels for presentation
    data.table::data.table(
      sample = c(rep("General Population Sample",3), rep("Cancer Sample",3)),
      lab = rep(c("Adjusted", "Unadjusted", "Scaled Density"), 2),
      x = c(0.43, 0.43, 0.43, 0.5, 0.5, 0.5),
      y = c(0.6, 0.25, 0.03, 0.67, 0.48, 0.03),
      size_col = base_size/ggplot2:::.pt,
      type = rep("presentation", 6)
    ),
    # labels for paper
    data.table::data.table(
      sample = c(rep("General Population Sample",3), rep("Cancer Sample",3)),
      lab = c("Average Monthly\n Spending",
              "Average Monthly Spending \n (Including Months After Death)",
              "Scaled Density",
              "Average Monthly\n Spending",
              "Average Monthly Spending \n (Including Months After Death)",
              "Scaled Density"),
      x = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5),
      y = c(0.47, 0.2, 0.01,0.67, 0.48, 0.03),
      size_col = inplot_text_size,
      type = rep("paper", 6)
    )
  )[, sample := factor(sample, levels=c("General Population Sample",
                                        "Cancer Sample"))]
  
  if (sample_n == "all") {
    sample_full_n = "General Population Sample"
  } else {
    sample_full_n = "Cancer Sample"
  }
  
  write.csv(dt_cost_by_phat[sample==sample_full_n],file = paste0("Spending_byPredictedMortality_", sample_n, "_", figs_type, ".csv"))
  
  pdf(file = paste0("Spending_byPredictedMortality_", sample_n, "_", figs_type, ".pdf"),width = 7.5, height = 7.5)
  # width = 7.5, height = 7.5)
  print(
    ggplot() + 
      geom_density(data = dt_prob_for_density[sample==sample_full_n], 
                   aes(x=prob_for_report, y=..scaled..),
                   fill = "gray80",
                   color = "gray80",
                   size = 0.7) +
      geom_line(data=dt_cost_by_phat[sample==sample_full_n],
                size=0.8, 
                aes(x=bins, 
                    y=ave_cost_future_365d/(round(max(dt_cost_by_phat$ave_cost_future_365d), 
                                                  -3)+1000), 
                    linetype = cost_type)) +
      geom_text(
        data    = text_dt[sample==sample_full_n & type == figs_type],
        mapping = aes(x = x, y = y, label = lab),
        size = max(text_dt[sample==sample_full_n & type == figs_type]$size_col)
      ) +
      scale_x_continuous(breaks = seq(0, 0.9, 0.1)) +
      scale_y_continuous(labels = function(x) {
        scales::comma(x*(round(max(dt_cost_by_phat$ave_cost_future_365d), -3)+1000))  
      }) +
      scale_linetype_discrete(guide = FALSE) +
      labs(x = "Initial Prognosis (One-Year Mortality Risk)", y = "Average Monthly Spending (NIS)")  +
      theme(aspect.ratio = 1.2,
            text = element_text(size = 24))
  )
  dev.off()
  
}

